package com.learn.springmvc.crontroller;

import com.learn.springmvc.domain.Car;
import com.learn.springmvc.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/hi")
public class MyMvc {

    /**
     * 测试model
     * @param model
     */
    @RequestMapping("/testModel")
    public String testModel(Model model){
        User user = new User();
        user.setAge(5);
        user.setName("赵四");
        model.addAttribute("user",user);
        System.out.println("你好model");
        return "success";
    }

    /**
     * 测试modelAndView
     * @return
     */
    @RequestMapping("/testModelView")
    public ModelAndView testModelView(){
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setName("王五");
        user.setAge(6);
        modelAndView.addObject("user",user);
        System.out.println("ModelAndView执行了");
        modelAndView.setViewName("success");
        return modelAndView;
    }


    @RequestMapping("/testRedirectForward")
    public String testRedirectForward(){
        System.out.println("转发或重定向");
        //重定向
        //return "redirect:/index.jsp";
        //转发
        return "forward:/WEB-INF/pages/success.jsp";
    }

    @RequestMapping("/unSync")
    public String unSync(@RequestBody String user){
        System.out.println(user);

//        user.setAge(666);
//        user.setName("李世民");
        return "success";
    }

    //测试拦截器
    @RequestMapping("/lanjie")
    public String lanjie(){
        System.out.println("拦截controller执行了");
        return "error";
    }
}
